<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>HTML5中的视频音频使用详解 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="HTML5,video,">
  

  <meta name="description" content="一、video 1.1 video支持视频格式  常见的视频格式 视频的组成部分：画面、音频、编码格式 视频编码：H.264、Theora、VP8(google开源)">
<meta name="keywords" content="HTML5,video">
<meta property="og:type" content="article">
<meta property="og:title" content="HTML5中的视频音频使用详解">
<meta property="og:url" content="http://blog.poetries.top/2016/12/30/video-audio/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、video 1.1 video支持视频格式  常见的视频格式 视频的组成部分：画面、音频、编码格式 视频编码：H.264、Theora、VP8(google开源)">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HTML5中的视频音频使用详解">
<meta name="twitter:description" content="一、video 1.1 video支持视频格式  常见的视频格式 视频的组成部分：画面、音频、编码格式 视频编码：H.264、Theora、VP8(google开源)">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、video"><span class="toc-text">一、video</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-video支持视频格式"><span class="toc-text">1.1 video支持视频格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-标签原型"><span class="toc-text">1.2 标签原型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-重要的video标签属性"><span class="toc-text">1.3 重要的video标签属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-Video-API"><span class="toc-text">1.4 Video  API</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-1-Video-方法"><span class="toc-text">1.4.1 Video 方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-2网络状态"><span class="toc-text">1.4.2网络状态</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-3-准备状态"><span class="toc-text">1.4.3 准备状态</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-4-回放状态"><span class="toc-text">1.4.4 回放状态</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-5-视频控制"><span class="toc-text">1.4.5 视频控制</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-6-相关事件"><span class="toc-text">1.4.6 相关事件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-7-其他"><span class="toc-text">1.4.7 其他</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、audio"><span class="toc-text">二、audio</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-audio格式"><span class="toc-text">2.1 audio格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-audio标签中的一些常用属性"><span class="toc-text">2.2 audio标签中的一些常用属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-api所提供的对audio标签操作的一些属性和方法"><span class="toc-text">2.3 api所提供的对audio标签操作的一些属性和方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-常用的控制用的函数"><span class="toc-text">2.4 常用的控制用的函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-audio标签API中的常用事件"><span class="toc-text">2.5 audio标签API中的常用事件</span></a></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-video-audio" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">HTML5中的视频音频使用详解</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2016.12.30</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 2.6k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 10分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、video"><a href="#一、video" class="headerlink" title="一、video"></a>一、video</h2><hr>
<h3 id="1-1-video支持视频格式"><a href="#1-1-video支持视频格式" class="headerlink" title="1.1 video支持视频格式"></a>1.1 video支持视频格式</h3><hr>
<ul>
<li>常见的视频格式<ul>
<li>视频的组成部分：画面、音频、编码格式</li>
<li>视频编码：<code>H.264</code>、<code>Theora</code>、<code>VP8(google开源)</code><a id="more"></a></li>
</ul>
</li>
<li><p><code>HTML5</code>支持的视频格式：</p>
<ul>
<li><code>Ogg</code><ul>
<li>带有<code>Theora</code>视频编码+<code>Vorbis</code>音频编码的<code>Ogg</code>文件<ul>
<li>支持的浏览器:<code>F</code>、<code>C</code>、<code>O</code></li>
</ul>
</li>
</ul>
</li>
<li><code>MEPG4</code><ul>
<li>带有<code>H.264</code>视频编码+<code>AAC</code>音频编码的<code>MPEG4</code>文件</li>
<li>支持的浏览器: <code>S</code>、<code>C</code></li>
</ul>
</li>
<li><code>WebM</code><ul>
<li>带有<code>VP8</code>视频编码+<code>Vorbis</code>音频编码的<code>WebM</code>格式</li>
<li>支持的浏览器: <code>I</code>、<code>F</code>、<code>C</code>、<code>O</code></li>
<li>劣势：视频少、转码器几乎没有，不好转码</li>
</ul>
</li>
</ul>
</li>
<li><p>想要<code>video</code>能自动填充慢父div的大小，只要给<code>video</code>标签加上<code>style=&quot;width= 100%; height=100%; object-fit: fill&quot;</code>即可</p>
</li>
</ul>
<h3 id="1-2-标签原型"><a href="#1-2-标签原型" class="headerlink" title="1.2 标签原型"></a>1.2 标签原型</h3><hr>
<ul>
<li>指定一种视频格式，不能播就提示</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">video</span> <span class="attr">id</span>=<span class="string">"media"</span> <span class="attr">src</span>=<span class="string">"examp.mp4"</span> <span class="attr">width</span>=<span class="string">"500"</span> </span></span><br><span class="line"><span class="tag"><span class="attr">poster</span>=<span class="string">"examp1.jpg"</span> &gt;</span>您的浏览器不支持video<span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>给定多种视频格式，浏览器根据自身支持程度选择播放哪一种</li>
<li>注意：多个<code>source</code>标签，浏览器会从第一个开始识别，如果第一个不被识别，则会继续识别第二个；如果第一个识别成功，则会直接播放第一种格式视频</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">video</span> <span class="attr">controls</span> = <span class="string">“controls”</span>&gt;</span></span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span>=<span class="string">”1.mp4”</span> <span class="attr">type</span>=<span class="string">”video/mp4”</span> /&gt;</span>  //src属性写到source标签中，要指定视频的type类型，例如MP4的即为type=“video/mp4”</span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span> = <span class="string">“2.ogg”</span> <span class="attr">type</span>=<span class="string">”video/ogg”</span> /&gt;</span>  //ogg格式</span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;<span class="name">source</span> <span class="attr">src</span>=<span class="string">”3.webm”</span> <span class="attr">type</span>=<span class="string">”video/webm”</span> /&gt;</span>   //webm格式</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-重要的video标签属性"><a href="#1-3-重要的video标签属性" class="headerlink" title="1.3 重要的video标签属性"></a>1.3 重要的video标签属性</h3><hr>
<table>
<thead>
<tr>
<th>属性(常用)</th>
<th>值</th>
<th>功能描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>controls</strong></td>
<td>controls</td>
<td>是否显示播放控件</td>
</tr>
<tr>
<td><strong>autoplay</strong></td>
<td>autoplay</td>
<td>设置是否打开浏览器后自动播放</td>
</tr>
<tr>
<td><strong>width</strong></td>
<td>Pilex（像素）</td>
<td>设置播放器的宽度</td>
</tr>
<tr>
<td><strong>height</strong></td>
<td>Pilex（像素）</td>
<td>设置播放器的高度</td>
</tr>
<tr>
<td><strong>loop</strong></td>
<td>loop</td>
<td>设置视频是否循环播放（即播放完后继续重新播放）</td>
</tr>
<tr>
<td><strong>preload</strong></td>
<td>preload</td>
<td>设置是否等加载完再播放</td>
</tr>
<tr>
<td><strong>src</strong></td>
<td>url</td>
<td>设置要播放视频的url地址</td>
</tr>
<tr>
<td><strong>poster</strong></td>
<td>imgurl</td>
<td>设置播放器初始默认显示图片</td>
</tr>
<tr>
<td><strong>autobuffer</strong></td>
<td>autobuffer</td>
<td>设置为浏览器缓冲方式，不设置autoply才有效</td>
</tr>
</tbody>
</table>
<ul>
<li>API 属性</li>
</ul>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>audioTracks</td>
<td>返回表示可用音轨的 AudioTrackList 对象</td>
</tr>
<tr>
<td>buffered</td>
<td>返回表示音频/视频已缓冲部分的 TimeRanges 对象</td>
</tr>
<tr>
<td>controller</td>
<td>返回表示音频/视频当前媒体控制器的 MediaController 对象</td>
</tr>
<tr>
<td>crossOrigin</td>
<td>设置或返回音频/视频的 CORS 设置</td>
</tr>
<tr>
<td>currentSrc</td>
<td>返回当前音频/视频的 URL</td>
</tr>
<tr>
<td>currentTime</td>
<td>设置或返回音频/视频中的当前播放位置（以秒计）</td>
</tr>
<tr>
<td>defaultMuted</td>
<td>设置或返回音频/视频默认是否静音</td>
</tr>
<tr>
<td>defaultPlaybackRate</td>
<td>设置或返回音频/视频的默认播放速度</td>
</tr>
<tr>
<td>duration</td>
<td>返回当前音频/视频的长度（以秒计）</td>
</tr>
<tr>
<td>ended</td>
<td>返回音频/视频的播放是否已结束</td>
</tr>
<tr>
<td>error</td>
<td>返回表示音频/视频错误状态的 MediaError 对象</td>
</tr>
<tr>
<td>mediaGroup</td>
<td>设置或返回音频/视频所属的组合（用于连接多个音频/视频元素）</td>
</tr>
<tr>
<td>muted</td>
<td>设置或返回音频/视频是否静音</td>
</tr>
<tr>
<td>networkState</td>
<td>返回音频/视频的当前网络状态</td>
</tr>
<tr>
<td>paused</td>
<td>设置或返回音频/视频是否暂停</td>
</tr>
<tr>
<td>playbackRate</td>
<td>设置或返回音频/视频播放的速度</td>
</tr>
<tr>
<td>played</td>
<td>返回表示音频/视频已播放部分的 TimeRanges 对象</td>
</tr>
<tr>
<td>readyState</td>
<td>返回音频/视频当前的就绪状态</td>
</tr>
<tr>
<td>seekable</td>
<td>返回表示音频/视频可寻址部分的 TimeRanges 对象</td>
</tr>
<tr>
<td>seeking</td>
<td>返回用户是否正在音频/视频中进行查找</td>
</tr>
<tr>
<td>startDate</td>
<td>返回表示当前时间偏移的 Date 对象</td>
</tr>
<tr>
<td>textTracks</td>
<td>返回表示可用文本轨道的 TextTrackList 对象</td>
</tr>
<tr>
<td>videoTracks</td>
<td>返回表示可用视频轨道的 VideoTrackList 对象</td>
</tr>
<tr>
<td>volume</td>
<td>设置或返回音频/视频的音量</td>
</tr>
</tbody>
</table>
<ul>
<li><p>常用API属性</p>
<ul>
<li><code>duration</code>：返回当前音频/视频的长度（以秒计） </li>
<li><code>paused</code>：设置或返回音频/视频是否暂停 </li>
<li><code>currentTime</code>：设置或返回音频/视频中的当前播放位置（以秒计）</li>
<li><code>ended</code>：返回音频/视频的播放是否已结束</li>
</ul>
<h3 id="1-4-Video-API"><a href="#1-4-Video-API" class="headerlink" title="1.4 Video  API"></a>1.4 Video  API</h3></li>
</ul>
<hr>
<h4 id="1-4-1-Video-方法"><a href="#1-4-1-Video-方法" class="headerlink" title="1.4.1 Video 方法"></a>1.4.1 Video 方法</h4><hr>
<table>
<thead>
<tr>
<th>API</th>
<th>事件说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>play</strong></td>
<td>video.play();    播放视频</td>
</tr>
<tr>
<td><strong>pause</strong></td>
<td>video.pause();  暂停播放视频</td>
</tr>
<tr>
<td><strong>load</strong></td>
<td>video.load();   将全部属性回复默认值，视频恢复重新开始状态</td>
</tr>
<tr>
<td><strong>canPlayType</strong></td>
<td>var support = videoid.canPlayType(‘video/mp4′);   <br><br>判断浏览器是否支持当前类型的视频格式<br><br>返回值：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;空字符串：不支持Maybe：可能支持Probably：完全支持</td>
</tr>
</tbody>
</table>
<ul>
<li><p>常用方法</p>
<ul>
<li><code>play()</code>：开始播放音频/视频 </li>
<li><code>pause()</code>：暂停当前播放的音频/视频</li>
</ul>
</li>
</ul>
<h4 id="1-4-2网络状态"><a href="#1-4-2网络状态" class="headerlink" title="1.4.2网络状态"></a>1.4.2网络状态</h4><hr>
<ul>
<li><p>获取video对象</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Media = <span class="built_in">document</span>.getElementById(<span class="string">"media"</span>);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><strong>Media.currentSrc</strong>; <ul>
<li>返回当前资源的URL</li>
</ul>
</li>
<li><strong>Media.src = value</strong>; <ul>
<li>返回或设置当前资源的URL</li>
</ul>
</li>
<li><strong>Media.canPlayType(type)</strong>; <ul>
<li>是否能播放某种格式的资源</li>
</ul>
</li>
<li><strong>Media.networkState</strong>; <ul>
<li>0.此元素未初始化 </li>
<li>1.正常但没有使用网络 </li>
<li>2.正在下载数据 </li>
<li>3.没有找到资源</li>
</ul>
</li>
<li><strong>Media.load()</strong>; <ul>
<li>重新加载src指定的资源</li>
</ul>
</li>
<li><strong>Media.buffered</strong>; <ul>
<li>返回已缓冲区域，TimeRanges</li>
</ul>
</li>
<li><strong>Media.preload</strong>; <ul>
<li>none:不预载 </li>
<li>metadata:预载资源信息 </li>
<li>auto:</li>
</ul>
</li>
</ul>
<h4 id="1-4-3-准备状态"><a href="#1-4-3-准备状态" class="headerlink" title="1.4.3 准备状态"></a>1.4.3 准备状态</h4><hr>
<ul>
<li><strong>Media.readyState</strong></li>
<li><strong>Media.seeking</strong>; <ul>
<li>是否正在seeking</li>
</ul>
</li>
</ul>
<h4 id="1-4-4-回放状态"><a href="#1-4-4-回放状态" class="headerlink" title="1.4.4 回放状态"></a>1.4.4 回放状态</h4><hr>
<ul>
<li><strong>Media.currentTime = value</strong>; <ul>
<li>当前播放的位置，赋值可改变位置</li>
</ul>
</li>
<li><strong>Media.startTime;</strong> <ul>
<li>一般为0，如果为流媒体或者不从0开始的资源，则不为0</li>
</ul>
</li>
<li><strong>Media.duration</strong>; <ul>
<li>当前资源长度 流返回无限</li>
</ul>
</li>
<li><strong>Media.paused</strong>; <ul>
<li>是否暂停</li>
</ul>
</li>
<li><strong>Media.defaultPlaybackRate = value</strong>;<ul>
<li>默认的回放速度，可以设置</li>
</ul>
</li>
<li><strong>Media.playbackRate = value</strong>;<ul>
<li>当前播放速度，设置后马上改变</li>
</ul>
</li>
<li><strong>Media.played</strong>; <ul>
<li>返回已经播放的区域，TimeRanges，关于此对象见下文</li>
</ul>
</li>
<li><strong>Media.seekable</strong>; <ul>
<li>返回可以seek的区域 TimeRanges</li>
</ul>
</li>
<li><strong>Media.ended</strong>; <ul>
<li>是否结束</li>
</ul>
</li>
<li><strong>Media.autoPlay</strong>; <ul>
<li>是否自动播放</li>
</ul>
</li>
<li><strong>Media.loop</strong>; <ul>
<li>是否循环播放</li>
</ul>
</li>
<li><strong>Media.play()</strong>; <ul>
<li>播放</li>
</ul>
</li>
<li><strong>Media.pause()</strong>; <ul>
<li>暂停</li>
</ul>
</li>
</ul>
<h4 id="1-4-5-视频控制"><a href="#1-4-5-视频控制" class="headerlink" title="1.4.5 视频控制"></a>1.4.5 视频控制</h4><hr>
<ul>
<li>Media.controls;//是否有默认控制条</li>
<li>Media.volume = value; //音量</li>
<li>Media.muted = value; //静音</li>
<li>TimeRanges(区域)对象</li>
<li>TimeRanges.length; //区域段数</li>
<li>TimeRanges.start(index) //第index段区域的开始位置</li>
<li>TimeRanges.end(index) //第index段区域的结束位置</li>
</ul>
<h4 id="1-4-6-相关事件"><a href="#1-4-6-相关事件" class="headerlink" title="1.4.6 相关事件"></a>1.4.6 相关事件</h4><hr>
<ul>
<li>首先绑定事件的话可以通过js中的addEventListener方法来绑定事件</li>
</ul>
<table>
<thead>
<tr>
<th>事件</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>loadstart</strong></td>
<td>客户端开始请求数据</td>
</tr>
<tr>
<td><strong>progress</strong></td>
<td>客户端正在请求数据</td>
</tr>
<tr>
<td><strong>suspend</strong></td>
<td>延迟下载</td>
</tr>
<tr>
<td><strong>abort</strong></td>
<td>客户端主动终止下载（不是因为错误引起）</td>
</tr>
<tr>
<td><strong>loadstart</strong></td>
<td>客户端开始请求数据</td>
</tr>
<tr>
<td><strong>progress</strong></td>
<td>客户端正在请求数据</td>
</tr>
<tr>
<td><strong>error</strong></td>
<td>请求数据时遇到错误</td>
</tr>
<tr>
<td><strong>stalled</strong></td>
<td>网速失速</td>
</tr>
<tr>
<td><strong>play</strong></td>
<td>play()和autoplay开始播放时触发</td>
</tr>
<tr>
<td><strong>pause</strong></td>
<td>pause()触发</td>
</tr>
<tr>
<td><strong>loadedmetadata</strong></td>
<td>成功获取资源长度</td>
</tr>
<tr>
<td><strong>loadeddata</strong></td>
<td>-</td>
</tr>
<tr>
<td><strong>waiting</strong></td>
<td>等待数据，并非错误</td>
</tr>
<tr>
<td><strong>playing</strong></td>
<td>开始回放</td>
</tr>
<tr>
<td><strong>canplay</strong></td>
<td>可以播放，但中途可能因为加载而暂停</td>
</tr>
<tr>
<td><strong>canplaythrough</strong></td>
<td>可以播放，歌曲全部加载完毕</td>
</tr>
<tr>
<td><strong>seeking</strong></td>
<td>寻找中</td>
</tr>
<tr>
<td><strong>seeked</strong></td>
<td>寻找完毕</td>
</tr>
<tr>
<td><strong>timeupdate</strong></td>
<td>播放时间改变</td>
</tr>
<tr>
<td><strong>ended</strong></td>
<td>播放结束</td>
</tr>
<tr>
<td><strong>ratechange</strong></td>
<td>播放速率改变</td>
</tr>
<tr>
<td><strong>durationchange</strong></td>
<td>资源长度改变</td>
</tr>
<tr>
<td><strong>volumechange</strong></td>
<td>音量改变</td>
</tr>
</tbody>
</table>
<ul>
<li>常用事件<ul>
<li><code>oncanplay</code>：当文件就绪可以开始播放时运行的脚本（缓冲已足够开始时）。</li>
<li><code>ontimeupdate</code>： 当播放位置改变时（比如当用户快进到媒介中一个不同的位置时）运行的脚本。 </li>
<li><code>onended</code>：当媒介已到达结尾时运行的脚本（可发送类似“感谢观看”之类的消息）。</li>
</ul>
</li>
</ul>
<h4 id="1-4-7-其他"><a href="#1-4-7-其他" class="headerlink" title="1.4.7 其他"></a>1.4.7 其他</h4><ul>
<li><p>全屏:   </p>
<ul>
<li><code>element.webkitRequestFullScreen();</code>( webkit)</li>
<li><code>element.mozRequestFullScreen();</code> (Firefox )</li>
<li><code>element.requestFullscreen();</code> (W3C )</li>
</ul>
</li>
<li><p>退出全屏：</p>
<ul>
<li><code>document.webkitCancelFullScreen()</code>; (webkit )</li>
<li><code>document.mozCancelFullScreen()</code>;  (Firefox)</li>
<li><code>document.exitFullscreen();</code> (W3C)</li>
</ul>
</li>
</ul>
<h2 id="二、audio"><a href="#二、audio" class="headerlink" title="二、audio"></a>二、audio</h2><hr>
<h3 id="2-1-audio格式"><a href="#2-1-audio格式" class="headerlink" title="2.1 audio格式"></a>2.1 audio格式</h3><hr>
<ul>
<li><p>常见的音频格式</p>
<ul>
<li>音频编码：<code>ACC</code>、<code>MP3</code>、<code>Vorbis</code></li>
</ul>
</li>
<li><p>HTML5支持的音频格式：</p>
<ul>
<li><code>Ogg</code> 免费    支持的浏览器:<code>C</code>、<code>F</code>、<code>O</code></li>
<li><code>MP3</code> 收费    支持的浏览器: <code>I</code>、<code>C</code>、<code>S</code></li>
<li><code>Wav</code> 收费    支持的浏览器: <code>F</code>、<code>O</code>、<code>S</code></li>
</ul>
</li>
</ul>
<h3 id="2-2-audio标签中的一些常用属性"><a href="#2-2-audio标签中的一些常用属性" class="headerlink" title="2.2 audio标签中的一些常用属性"></a>2.2 audio标签中的一些常用属性</h3><table>
<thead>
<tr>
<th>属性</th>
<th>属性值</th>
<th>注释</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>src</strong></td>
<td>url</td>
<td>播放的音乐的url地址（火狐只支持ogg的音乐，而IE9只支持MP3格式的音乐。chrome貌似全支持）</td>
</tr>
<tr>
<td><strong>preload</strong></td>
<td>preload</td>
<td>预加载（在页面被加载时进行加载或者说缓冲音频），如果使用了autoplay的话那么该属性失效。</td>
</tr>
<tr>
<td><strong>loop</strong></td>
<td>loop</td>
<td>循环播放</td>
</tr>
<tr>
<td><strong>controls</strong></td>
<td>controls</td>
<td>是否显示默认控制条（控制按钮）</td>
</tr>
<tr>
<td><strong>autoplay</strong></td>
<td>autoplay</td>
<td>自动播放</td>
</tr>
</tbody>
</table>
<ul>
<li>对于音乐格式的支持</li>
</ul>
<table>
<thead>
<tr>
<th>音频格式</th>
<th>Chrome</th>
<th>Firefox</th>
<th>IE9</th>
<th>Opera</th>
<th>Safari</th>
</tr>
</thead>
<tbody>
<tr>
<td>OGG</td>
<td>支持</td>
<td>支持</td>
<td>支持</td>
<td>不支持</td>
<td>不支持</td>
</tr>
<tr>
<td>MP3</td>
<td>支持</td>
<td>不支持</td>
<td>支持</td>
<td>不支持</td>
<td>支持</td>
</tr>
<tr>
<td>WAV</td>
<td>不支持</td>
<td>支持</td>
<td>不支持</td>
<td>支持</td>
<td>不支持</td>
</tr>
</tbody>
</table>
<ul>
<li>audio可通过new来创建。也可以通过用document来获取</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//通过new关键字来创建Audio对象</span></span><br><span class="line"><span class="keyword">var</span> Music = <span class="keyword">new</span> Audio(<span class="string">"test.mp3"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//通过document来获取已经存在的Audio对象</span></span><br><span class="line"><span class="keyword">var</span> Music = <span class="built_in">document</span>.getElementById(<span class="string">"audio"</span>);</span><br><span class="line"><span class="comment">//当然这里也可以使用document.getElementsByClassName('className')等其他的方法来获取。</span></span><br></pre></td></tr></table></figure>
<h3 id="2-3-api所提供的对audio标签操作的一些属性和方法"><a href="#2-3-api所提供的对audio标签操作的一些属性和方法" class="headerlink" title="2.3 api所提供的对audio标签操作的一些属性和方法"></a>2.3 api所提供的对audio标签操作的一些属性和方法</h3><hr>
<table>
<thead>
<tr>
<th>属性</th>
<th>注释</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>duration</strong></td>
<td>获取媒体文件的总时长，以s为单位，如果无法获取，返回NaN</td>
</tr>
<tr>
<td><strong>paused</strong></td>
<td>如果媒体文件被暂停，那么paused属性返回true，反之则返回false</td>
</tr>
<tr>
<td><strong>ended</strong></td>
<td>如果媒体文件播放完毕返回true</td>
</tr>
<tr>
<td><strong>muted</strong></td>
<td>用来获取或设置静音状态。值为boolean</td>
</tr>
<tr>
<td><strong>volume</strong></td>
<td>控制音量的属性值为0-1;0为音量最小，1为音量最大</td>
</tr>
<tr>
<td><strong>startTime</strong></td>
<td>返回起始播放时间</td>
</tr>
<tr>
<td><strong>error</strong></td>
<td>返回错误代码，为uull的时候为正常。否则可以通过Music.error.code来获取</td>
</tr>
<tr>
<td><strong>currentTime</strong></td>
<td>用来获取或控制当前播放的时间，单位为s。</td>
</tr>
<tr>
<td><strong>currentSrc</strong></td>
<td>以字符串形式返回正在播放或已加载的文件</td>
</tr>
</tbody>
</table>
<h3 id="2-4-常用的控制用的函数"><a href="#2-4-常用的控制用的函数" class="headerlink" title="2.4 常用的控制用的函数"></a>2.4 常用的控制用的函数</h3><hr>
<table>
<thead>
<tr>
<th>函数</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>load()</strong></td>
<td>加载音频、视频软件</td>
</tr>
<tr>
<td><strong>play()</strong></td>
<td>加载并播放音频、视频文件或重新播放暂停的的音频、视频</td>
</tr>
<tr>
<td><strong>pause()</strong></td>
<td>暂停出于播放状态的音频、视频文件</td>
</tr>
<tr>
<td><strong>canPlayType(obj)</strong></td>
<td>测试是否支持给定的Mini类型的文件</td>
</tr>
</tbody>
</table>
<h3 id="2-5-audio标签API中的常用事件"><a href="#2-5-audio标签API中的常用事件" class="headerlink" title="2.5 audio标签API中的常用事件"></a>2.5 audio标签API中的常用事件</h3><hr>
<ul>
<li>首先绑定事件的话可以通过js中的addEventListener方法来绑定事件</li>
</ul>
<table>
<thead>
<tr>
<th>事件名称</th>
<th>事件作用</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>loadstart</strong></td>
<td>客户端开始请求数据</td>
</tr>
<tr>
<td><strong>progress</strong></td>
<td>客户端正在请求数据（或者说正在缓冲）</td>
</tr>
<tr>
<td><strong>play</strong></td>
<td>play()和autoplay播放时</td>
</tr>
<tr>
<td><strong>pause</strong></td>
<td>pause()方法促发时</td>
</tr>
<tr>
<td><strong>ended</strong></td>
<td>当前播放结束</td>
</tr>
<tr>
<td><strong>timeupdate</strong></td>
<td>当前播放时间发生改变的时候。播放中常用的时间处理</td>
</tr>
<tr>
<td><strong>canplaythrough</strong></td>
<td>歌曲已经载入完全完成</td>
</tr>
<tr>
<td><strong>canplay</strong></td>
<td>缓冲至目前可播放状态。</td>
</tr>
</tbody>
</table>
<p>其实<code>video</code>的<code>api</code>和<code>audio</code>几乎一致。稍稍有点不同。所以基本上会了一个其他的也就都会了</p>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2016/12/13/js-props/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2017/01/10/js-scope/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
